﻿using WordCloudClient.Models.Entities;
using DbType = SqlSugar.DbType;

namespace WordCloudClient.Services
{
    public class SystemService : ISystemService
    {
        readonly SqlSugarClient db = new(new ConnectionConfig()
        {
            ConnectionString = @"DataSource=.\Data\WordCloudDB.db",
            DbType = DbType.Sqlite,
            IsAutoCloseConnection = true
        });
        #region FilterWords
        public bool IsExistFilterWords(string word)
        {
            return db.Queryable<FilterWordsEntity>().Any(it => it.Word == word && it.IsEnable == 1);
        }
        public List<FilterWordsEntity> GetFilterWordsEntities(QueryModel queryModel)
        {
            Expressionable<FilterWordsEntity> exp = Expressionable.Create<FilterWordsEntity>()
               .AndIF(!string.IsNullOrWhiteSpace(queryModel.Keyword), it => it.Word.Contains(queryModel.Keyword) || it.Remark.Contains(queryModel.Keyword));
            return db.Queryable<FilterWordsEntity>().Where(exp.ToExpression()).ToList();
        }
        public List<FilterWordsEntity> GetFilterWordsEntities(QueryModel queryModel, ref int totalPages, ref int totalCount)
        {
            Expressionable<FilterWordsEntity> exp = Expressionable.Create<FilterWordsEntity>()
                .AndIF(!string.IsNullOrWhiteSpace(queryModel.Keyword), it => it.Word.Contains(queryModel.Keyword) || it.Remark.Contains(queryModel.Keyword));
            return db.Queryable<FilterWordsEntity>().Where(exp.ToExpression()).ToPageList(queryModel.PageIndex, queryModel.PageSize, ref totalCount, ref totalPages);
        }
        public bool UpdateFilterWord(FilterWordsEntity filterWordsEntity, ref string errorMsg)
        {
            if (db.Queryable<FilterWordsEntity>().Any(it => it.Word == filterWordsEntity.Word && it.Id != filterWordsEntity.Id))
            {
                errorMsg = $"{filterWordsEntity.Word}已存在相同的单词！";
                return false;
            }
            return db.Updateable(filterWordsEntity).ExecuteCommand() > 0;
        }
        public bool AddFilterWord(FilterWordsEntity filterWordsEntity, ref string errorMsg)
        {
            if (db.Queryable<FilterWordsEntity>().Any(it => it.Word == filterWordsEntity.Word))
            {
                errorMsg = $"{filterWordsEntity.Word}已存在相同的单词！";
                return false;
            }
            return db.Insertable(filterWordsEntity).ExecuteCommand() > 0;
        }
        public bool DeleteFilterWord(FilterWordsEntity filterWordsEntity)
        {
            return db.Deleteable<FilterWordsEntity>().Where(new FilterWordsEntity() { Id = filterWordsEntity.Id }).ExecuteCommand() > 0;
        }
        #endregion

        #region SystemSetting
        public List<DictionaryEntity> GetDictionaryEntities(QueryModel queryModel)
        {
            Expressionable<DictionaryEntity> exp = Expressionable.Create<DictionaryEntity>()
                .AndIF(!string.IsNullOrWhiteSpace(queryModel.Keyword), it => it.Name.Contains(queryModel.Keyword) ||
                it.Code.Contains(queryModel.Keyword) || it.Remark.Contains(queryModel.Keyword) || it.Value.Contains(queryModel.Keyword));
            return db.Queryable<DictionaryEntity>().Where(exp.ToExpression()).ToList();
        }
        public List<DictionaryEntity> GetDictionaryEntities(QueryModel queryModel, ref int totalPages, ref int totalCount)
        {
            Expressionable<DictionaryEntity> exp = Expressionable.Create<DictionaryEntity>()
                .AndIF(!string.IsNullOrWhiteSpace(queryModel.Keyword), it => it.Name.Contains(queryModel.Keyword) ||
                it.Code.Contains(queryModel.Keyword) || it.Remark.Contains(queryModel.Keyword) || it.Value.Contains(queryModel.Keyword));
            return db.Queryable<DictionaryEntity>().Where(exp.ToExpression()).ToPageList(queryModel.PageIndex, queryModel.PageSize, ref totalCount, ref totalPages);
        }
        public bool UpdateDictionary(DictionaryEntity dictionaryEntity, ref string errorMsg)
        {
            if (db.Queryable<DictionaryEntity>().Any(it => it.Name == dictionaryEntity.Name && it.Id != dictionaryEntity.Id))
            {
                errorMsg = $"{dictionaryEntity.Name}已存在相同的字典名称！";
                return false;
            }
            if (db.Queryable<DictionaryEntity>().Any(it => it.Code == dictionaryEntity.Code && it.Id != dictionaryEntity.Id))
            {
                errorMsg = $"{dictionaryEntity.Name}已存在相同的字典编码！";
                return false;
            }
            return db.Updateable(dictionaryEntity).ExecuteCommand() > 0;
        }
        public bool AddDictionary(DictionaryEntity dictionaryEntity, ref string errorMsg)
        {
            if (db.Queryable<DictionaryEntity>().Any(it => it.Name == dictionaryEntity.Name))
            {
                errorMsg = $"{dictionaryEntity.Name}已存在相同的字典名称！";
                return false;
            }
            if (db.Queryable<DictionaryEntity>().Any(it => it.Code == dictionaryEntity.Code))
            {
                errorMsg = $"{dictionaryEntity.Name}已存在相同的字典编码！";
                return false;
            }
            return db.Insertable(dictionaryEntity).ExecuteCommand() > 0;
        }
        public bool DeleteDictionary(DictionaryEntity dictionaryEntity)
        {
            return db.Deleteable<DictionaryEntity>().Where(new DictionaryEntity() { Id = dictionaryEntity.Id }).ExecuteCommand() > 0;
        }

        public DictionaryEntity GetDictionaryValueByCode(string code)
        {
            DictionaryEntity dictionaryEntity = db.Queryable<DictionaryEntity>().Where(it => it.Code == code).First();
            return dictionaryEntity == null ? new DictionaryEntity() : dictionaryEntity;
        }
        #endregion
    }
}
